package com.gxa.service.discrepancy.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.entity.discrepancy.Alarm;
import com.gxa.entity.discrepancy.DeviceLog;
import com.gxa.entity.discrepancy.search.AlarmSearchMap;
import com.gxa.mapper.discrepancy.AlarmMapper;
import com.gxa.service.discrepancy.AlarmService;
import com.gxa.service.discrepancy.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:43:53
 */
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, Alarm> implements AlarmService {

    @Autowired
    private AlarmMapper mapper;
    @Autowired
    private DeviceService deviceService;
    @Override
    public PageInfo<Alarm> queryByPage(AlarmSearchMap map) {
        PageHelper.startPage(map.getPageNum(),map.getPageSize());//1.开启分页

        QueryWrapper<Alarm> wrapper = new QueryWrapper<>();//2.条件构造器 模拟写动态sql  或者在xml 中写
        if (!StringUtils.isAnyBlank(map.getUserName())) {
            wrapper.like("user_name", map.getUserName());
        }
        if (!StringUtils.isAnyBlank(map.getDeviceName())) {
            wrapper.like("device_name", map.getDeviceName());
        }
        if (!StringUtils.isAnyBlank(map.getLocation())) {
            wrapper.like("location", map.getLocation());
        }
        if(!StringUtils.isAnyBlank(map.getStartTime(),map.getEndTime())){
            wrapper.between("alarm_time",map.getStartTime(),map.getEndTime());
        }
        PageInfo<Alarm> pageInfo = new PageInfo<>(mapper.selectByPage(wrapper));//3.获取分页信息
        return pageInfo;
    }

    @Override
    public Set<String> getDevices() {
        return deviceService.getDevices();
    }

    @Override
    public Set<String> getLocations() {
        return deviceService.getLocations();
    }
}
